﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using DonnetSpeech;

namespace MINERP.COM
{
    public class Speech
    {

        public Speech()
        {
            dlSpeak = SpeekMsg;//发音

            speakThread = new Thread(new ThreadStart(SpeakMsg));
            speakThread.Start();
        }
        SpeekMsgDelegate dlSpeak;//发音
        Thread speakThread = null;
        private Thread tesThread = null;
        Queue SpeakQueue = new Queue();
        SpVoice Voice = new SpVoice();
        SpeechVoiceSpeakFlags SpFlags;
        private delegate void SpeekMsgDelegate(string strMsg, bool isNeedReadCompleted);

        private struct SpeekMsgStruct
        {
            public string msg { get; set; }
            public bool isNeedReadCompleted;
        }

        private void SpeakMsg()
        {
            while (true)
            {
                if (SpeakQueue.Count > 0)
                {
                    SpeekMsgStruct currMsgStruct = (SpeekMsgStruct)SpeakQueue.Dequeue();

                    SpeekMsg(currMsgStruct.msg, currMsgStruct.isNeedReadCompleted);
                }

                Thread.Sleep(100);

            }
        }
        /// <summary>
        /// 发音
        /// </summary>
        /// <param name="strMsg"></param>
        private void SpeekMsg(string strMsg, bool isNeedReadCompleted)
        {
            //更新界面的显示,"A1,此单已完成"
            string gridName = strMsg.Split(new char[] { ',' })[0];

            //Application.DoEvents();            




            if (isNeedReadCompleted)
            {
                Voice.Priority = SpeechVoicePriority.SVPAlert;
                SpFlags = SpeechVoiceSpeakFlags.SVSFDefault;

            }
            else
            {
                SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
            }
          
            Voice.Speak(strMsg.Substring(1), SpFlags);//将A去掉直接读格子号



        }

        private void SpeekMsg(string strMsg)
        {
            Voice.Speak(strMsg,SpeechVoiceSpeakFlags.SVSFDefault);
        }

        public void Speak(string Msg)
        {
            SpeakQueue.Enqueue(new SpeekMsgStruct() { msg = " "+Msg, isNeedReadCompleted = true});
        }
        public void Speak(string Msg,bool isComplete)
        {
            SpeakQueue.Enqueue(new SpeekMsgStruct() { msg = Msg, isNeedReadCompleted = isComplete });
        }
    }
}
